<?php
/**
 * @since v1.0
 * @modify 2015.6.21
 * @copyright 深圳亿游
 */
require_once(LIB_PATH.'controller/root.con.php');

class questionCon extends rootCon
{
    private function getAgeByBirthday($date){
        $time = time()-strtotime($date);
        return $time/(3600*24*365);
    }


    public function resultAction(){
        $uid = mylogin::getUid(true);
        $studentId = r_int('studentId');
        $student = new Student();
        $student->find($studentId);
        if($uid != $student->uid){
            $this->j(-1,"参数错误",['studentId'=>$studentId]);
        }
        $age = $this->getAgeByBirthday($student->birthday);
        if($age > 21) $age = 21;
        $ageSql = " AND  minAge <=$age AND maxAge>=$age ";

        if(!$studentId) $this->j(-1,"缺少参数",['studentId'=>$studentId]);
        $questionStudent = new QuestionStudent();
        $sql = "SELECT questionTypeId FROM ".$questionStudent->table()." WHERE studentId=".$studentId." ORDER BY questionTypeId DESC LIMIT 1";
        $one = $questionStudent->one($sql);

        $questionDimension = new QuestionDimension();
        $dimensionAll = $questionDimension->findAll(['status'=>STATUS_PUBLISH ,'questionTypeId'=>$one['questionTypeId']]);

        $rect['dimensionList'] = $dimensionAll;



        $course = new Course();
        $sql = "SELECT * FROM ".$course->table()." WHERE status=".STATUS_PUBLISH." AND courseName IS NOT NULL $ageSql";//
        $courseList  = $course->pager($sql,20," ORDER BY RAND() ");
        $i = 0;
        foreach($courseList['list'] as $item){
            $schoolId = $courseList['list'][$i]['schoolId'];
            $schoolObj = new School();
            $schoolObj->find($schoolId);
            $courseList['list'][$i]['schoolName'] = $schoolObj->schoolName;
            $i++;
        }
        $rect['recommendList'] = $courseList;

        $this->j(0,'',$rect);
    }

    public function getAction(){
        $typeId = r_int('questionTypeId');
        if(!$typeId) $this->j(-1,"缺少参数",['questionTypeId'=>$typeId]);

        $question = new Question();
        $sql = "SELECT q.id,q.questionTypeId,q.subjectId,q.questionDimensionId,q.sort,q.title,q.option1,q.option2,q.option3,q.option4,q.status,d.dimension FROM ".$question->table()." q LEFT JOIN tbl_question_dimension d ON q.questionDimensionId=d.questionDimensionId WHERE q.questionTypeId=".$typeId." AND q.status=".STATUS_PUBLISH." ORDER BY q.sort ASC,q.id ASC";
        $all = $question->query($sql);
        $this->j(0,'',$question->hideList($all));
    }

    public function saveScoreAction(){
        $uid = mylogin::getUid(true);
        $studentId = r_int('studentId');
        //检查是否我的学生
        $student = new Student();
        $student->find($studentId);
        if($uid != $student->uid){
            $this->j(-1,"参数错误",['studentId'=>$studentId]);
        }

        $answer = r('answer');
        if(!is_array($answer)){
            $this->j(-2,"参数错误",["answer"=>$answer]);
        }

        foreach($answer as $item){
            $score = new QuestionStudent();
            $score->studentId = $studentId;
            $score->questionDimensionId = $item->questionDimensionId;

            if(!$score->questionDimensionId) continue;

            $find = $score->find();

            $dimension = new QuestionDimension();
            $dimension->find($score->questionDimensionId);
            $score->questionTypeId = $dimension->questionTypeId;
            $score->dimension = $dimension->dimension;
            $score->score = $item->score;
            $saved = $score->save();
        }

        $this->j(0,'',['saved'=>$saved]);
    }

    public function showScoreAction(){
        $uid = mylogin::getUid(true);
        $student = new Student();
        $allStudent = $student->all(['uid',$uid]);
        if(!is_array($allStudent)) $this->j(0,"没有学生数据",[]);

        $i = 0;
        foreach($allStudent as $student){
            $studentId = $student['studentId'];
            if(!$student['studentImg'])  $allStudent[$i]['studentImg'] = "/img/addstudent.png";

            $studentScore = new QuestionStudent();
            $sql = "SELECT * FROM ".$studentScore->table()." WHERE studentId=$studentId AND questionDimensionId>0 ORDER BY score ASC ";
            $scoreAll = $studentScore->query($sql);
            $j = 0;
            foreach($scoreAll as $item){
                $scoreAll[$j]['score'] = intval($item['score']);
                $j++;
            }
            $allStudent[$i]['score'] = $scoreAll;
            $i++;
        }

        $this->j(0,"评测结果",$allStudent);
    }
}



